package com.hbx.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbx.eduservice.entity.EduChapter;
import com.hbx.eduservice.entity.EduCourse;
import com.hbx.eduservice.entity.EduCourseDescription;
import com.hbx.eduservice.entity.EduVideo;
import com.hbx.eduservice.entity.chapter.ChapterVo;
import com.hbx.eduservice.entity.dto.CourseInfoVo;
import com.hbx.eduservice.entity.dto.CoursePublishVo;
import com.hbx.eduservice.entity.frontvo.CourseFrontVo;
import com.hbx.eduservice.entity.frontvo.CourseWebVo;
import com.hbx.eduservice.mapper.EduCourseMapper;
import com.hbx.eduservice.service.EduChapterService;
import com.hbx.eduservice.service.EduCourseDescriptionService;
import com.hbx.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbx.eduservice.service.EduVideoService;
import com.hbx.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author hbx
 * @since 2023-10-02
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    //添加课程基本信息
    @Override
    @Transactional
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //向课程表添加基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        boolean insert = this.save(eduCourse);

        if(!insert){
            throw new GuliException(20001,"添加课程信息失败");
        }
        String Cid = eduCourse.getId();
        //向课程简介表添加课程简介
        EduCourseDescription description = new EduCourseDescription();
        description.setDescription(courseInfoVo.getDescription());
        description.setId(Cid);
        eduCourseDescriptionService.save(description);
        return Cid;
    }

    @Override
    @Transactional
    public CourseInfoVo getCourseInfo(String courseId) {
        //查询课程表
        EduCourse eduCourse = this.getById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //查询描述表
        EduCourseDescription courseDescription = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    @Transactional
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        boolean b = this.updateById(eduCourse);
        if(!b){
            throw new GuliException(20001,"修改课程信息失败");
        }
        //修改描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescription.setDescription(eduCourseDescription.getDescription());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    //查询课程发布信息
    @Override
    public CoursePublishVo getPublishCourseInfo(String id) {
        CoursePublishVo info = baseMapper.getPublishCourseInfo(id);
        return info;
    }

    //删除课程（删除所有相关联的）
    @Override
    public void removeCourse(String courseId) {
        //删除小节
        videoService.removeVideoByCourseId(courseId);
        //删除章节
        LambdaQueryWrapper<EduChapter> chapterQuery = new LambdaQueryWrapper<>();
        chapterQuery.eq(EduChapter::getCourseId,courseId);
        chapterService.remove(chapterQuery);
        //删除简介
        courseDescriptionService.removeById(courseId);
        //删除课程
        boolean flag = this.removeById(courseId);
        if(!flag)
            throw new GuliException(20001,"删除课程失败");
    }
    //条件分页查询课程
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        LambdaQueryWrapper<EduCourse> queryWrapper = new LambdaQueryWrapper<>();
        //一级分类
        queryWrapper.eq(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId()),EduCourse::getSubjectParentId,courseFrontVo.getSubjectParentId());
        //二级分类
        queryWrapper.eq(!StringUtils.isEmpty(courseFrontVo.getSubjectId()),EduCourse::getSubjectId,courseFrontVo.getSubjectId());
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){//价格排序
            queryWrapper.orderByDesc(EduCourse::getPrice);
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){//销售量排序
            queryWrapper.orderByDesc(EduCourse::getBuyCount);
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){//创建时间排序
            queryWrapper.orderByDesc(EduCourse::getGmtCreate);
        }
        this.page(pageParam,queryWrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {

        return baseMapper.getBaseCourseInfoMapper(courseId);
    }
}
